#include "StdAfx.h"
#include "ImagePanelUI.h"

CImagePanelUI::CImagePanelUI(void) : m_hBitmap(NULL)
{
	m_cxyFixed.cx = m_cxyFixed.cy = 0;
}

CImagePanelUI::~CImagePanelUI(void)
{
	if( m_hBitmap != NULL ) ::DeleteObject(m_hBitmap);
}

bool CImagePanelUI::SetImage(LPCTSTR pstrBitmap)
{
	if( m_hBitmap != NULL ) ::DeleteObject(m_hBitmap);
	m_hBitmap = (HBITMAP) ::LoadImage(m_pManager->GetResourceInstance(), pstrBitmap, IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION);
	ASSERT(m_hBitmap);
	if( m_hBitmap == NULL ) return false;
	BITMAP bm = { 0 };
	::GetObject(m_hBitmap, sizeof(BITMAP), &bm);
	SetWidth(bm.bmWidth);
	SetHeight(bm.bmHeight);
	return true;
}

void CImagePanelUI::SetWidth(int cxWidth)
{
	m_cxyFixed.cx = cxWidth;
	UpdateLayout();
}

void CImagePanelUI::SetHeight(int cyHeight)
{
	m_cxyFixed.cy = cyHeight;
	UpdateLayout();
}

void CImagePanelUI::SetAttribute(LPCTSTR pstrName, LPCTSTR pstrValue)
{
	if( _tcscmp(pstrName, _T("width")) == 0 ) SetWidth(_ttoi(pstrValue));
	else if( _tcscmp(pstrName, _T("height")) == 0 ) SetHeight(_ttoi(pstrValue));
	else if( _tcscmp(pstrName, _T("image")) == 0 ) SetImage(pstrValue);
	else CControlUI::SetAttribute(pstrName, pstrValue);
}

LPCTSTR CImagePanelUI::GetClass() const
{
	return _T("ImagePanel");
}

SIZE CImagePanelUI::EstimateSize(SIZE szAvailable)
{
	return m_cxyFixed;
}

void CImagePanelUI::DoPaint(HDC hDC, const RECT& rcPaint)
{
	CBlueRenderEngineUI::DoPaintBitmap(hDC, m_pManager, m_hBitmap, m_rcItem);
}
